//
//  ICChatMessageTextCell.m
//  ICome
//
//  Created by ENN on 16/3/13.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICChatMessageTextCell.h"
#import "ICMessageModel.h"
#import "ICFaceManager.h"
#import "ICHeaderFile.h"

@interface ICChatMessageTextCell ()<MLLinkLabelDelegate>

@end

@implementation ICChatMessageTextCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.chatLabel];
    }
    return self;
}




#pragma mark - Private Method


- (void)setModelFrame:(ICMessageFrame *)modelFrame
{
    [super setModelFrame:modelFrame];
    self.chatLabel.frame = modelFrame.chatLabelF;
    [self.chatLabel setAttributedText:[ICFaceManager transferMessageString:modelFrame.model.message.content font:self.chatLabel.font lineHeight:self.chatLabel.font.lineHeight]];
    //URL 手机号加线
    if (modelFrame.model.isSender) {
        self.chatLabel.textColor = MESSAGEDEFAULTCOLOR;
        self.chatLabel.linkTextAttributes = @{NSForegroundColorAttributeName:MESSAGEURLDEFAULTCOLOR,NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)};
    }else{
        self.chatLabel.textColor = MESSAGEDEFAULTCOLOR;

//        self.chatLabel.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor colorWithRed:0.0/255.0 green:162.0/255.0 blue:213.0/255.0 alpha:1.0],NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)};
//
        
        self.chatLabel.linkTextAttributes = @{NSForegroundColorAttributeName:MESSAGEURLDEFAULTCOLOR,NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)};

    }
}


- (void)attemptOpenURL:(NSURL *)url
{
    BOOL safariCompatible = [url.scheme isEqualToString:@"http"] || [url.scheme isEqualToString:@"https"];
    if (safariCompatible && [[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警示" message:@"您的链接无效" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}

- (void)urlSkip:(NSString *)url
{
    [self routerEventWithName:GXRouterEventURLSkip
                     userInfo:@{@"url"   : url
                                }];
}




#pragma mark - Getter and Setter
- (MLLinkLabel *)chatLabel
{
    if (!_chatLabel) {
        _chatLabel = [[MLLinkLabel alloc] init];
        _chatLabel.numberOfLines = 0;
        _chatLabel.font = MessageFont;
        _chatLabel.lineHeightMultiple = 1.0f;
        _chatLabel.beforeAddLinkBlock = nil;
        _chatLabel.allowLineBreakInsideLinks = NO;
        _chatLabel.linkTextAttributes = nil;
        _chatLabel.activeLinkTextAttributes = nil;
        
        _chatLabel.dataDetectorTypes = MLDataDetectorTypeURL|MLDataDetectorTypePhoneNumber;
        _chatLabel.delegate = self;
    }
    return _chatLabel;
}

/** 聊天文本链接URL iphone控制*/
- (void)didClickLink:(MLLink*)link linkText:(NSString*)linkText linkLabel:(MLLinkLabel*)linkLabel {
    if (link.linkType == MLLinkTypeURL) {
        [self urlSkip:link.linkValue];
    }else if(link.linkType == MLLinkTypePhoneNumber){
         [ICTools tel:link.linkValue];
    }else if (link.linkType == MLLinkTypeUserHandle) {
        
    }
}




@end
